<?php
namespace app\tuchuang\controller;

use app\common\controller\Front;

class Index extends Front
{

    private $imageUrl = '';

	public function _initialize()
    {
        $this->imageUrl = DcEmpty(config('tuchuang.link_default'), 'https://cdn.daicuo.cc/images/gzh/dc_01.jpg');
        
		parent::_initialize();
	}
		
	public function index()
    {
        if( $this->request->isAjax() ){
        
            config('common.app_domain',true);
            
            if(!$url = input('request.url/s')){
                return json(['code'=>0,'msg'=>'请填写待加速的图片地址','data'=>'']);
            }
            
            $url = $this->urlEncode($url);
            
            return json(['code'=>1,'msg'=>'ok','data'=>DcUrl('tuchuang/index/read',['url'=>$url],'')]);
        }
		return $this->fetch();
	}
    
    public function read()
    {
        
        $img_url = $this->urlDecode(input('request.url/s'));

        //验证协议
        $args = parse_url($img_url);
        if(!in_array($args['scheme'], array('http','https','ftp'))){
            $this->redirect($img_url, 302);
        }
        
        //验证访问域名
        if( config('tuchuang.link_domain') ){
            $referer = parse_url(input('server.HTTP_REFERER')); 
            $domains = explode(',', config('tuchuang.link_domain'));
            if( !in_array($referer['host'], $domains) ){
                $this->redirect($img_url, 302);
            }
        }
        
        //验证加速域名
        if( config('tuchuang.link_referer') ){
            $referers = explode(',', config('tuchuang.link_referer'));
            if( !in_array($args['host'], $referers) ){
                $this->redirect($img_url, 302);
            }
        }
        
        //根据后缀设置header
        $suffix = DcEmpty(strrchr($img_url,"."), '.jpg');
        if($suffix == '.jpg'){
            header('Content-type: image/jpeg');
        }else if($suffix == '.jpeg'){
            header('Content-type: image/jpeg');
        }else if($suffix == '.gif'){
            header('Content-type: image/gif');
        }else if($suffix == '.bmp'){
            header('Content-type: application/x-bmp');
        }else if($suffix == '.svg'){
            header('Content-type: text/xml');
        }else{
            header('Content-type: image/png');
        }
        //优先缓存读取
        $cache_name = md5($img_url);
        $cache_time = config('cache.expire_detail');
        if( $cache_time >0 ){
            if($imgFile = DcCache($cache_name)){
                exit($imgFile);
            }
        }
        //远程获取及缓存
        $imgFile = DcCurl('auto', 5, $img_url);
        if( $cache_time && $imgFile ){
            DcCache($cache_name, $cache_name, intval($cache_time));
        }
        //是否写入数据成功
        if($imgFile){
            exit($imgFile);
        }
        //默认操作
        $this->redirect($img_url, 302);
	}
    
    //图片地址加密处理
    private function urlEncode($imgUrl){
        if(!$imgUrl){
            return $this->imageUrl;
        }
        if( config('tuchuang.link_encode') ){
            foreach( explode(',', config('tuchuang.link_encode')) as $key=>$function){
                $imgUrl = $function($imgUrl);
            }
        }
        return $imgUrl;
    }
    
    //图片地址解密处理
    private function urlDecode($imgUrl){
        if(!$imgUrl){
            return $this->imageUrl;
        }
        
        //$imgUrl = urldecode($imgUrl);
        
        if( config('tuchuang.link_decode') ){
            foreach( explode(',', config('tuchuang.link_decode')) as $key=>$function){
                $imgUrl = $function($imgUrl);
            }
        }
        return $imgUrl;
    }
    
    public function _empty($name)
    {
        return DcHtml($name);
	}
}